package timeitj.ejb;

import java.util.List;
import javax.annotation.Resource;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;
import timeitj.ejb.invitation.InvitationManager;
import timeitj.ejb.participance.ParticipanceManager;
import timeitj.ejb.persons.PersonManager;
import timeitj.ejb.projects.ProjectManager;
import timeitj.persistence.Invitation;
import timeitj.persistence.Participance;
import timeitj.persistence.Person;
import timeitj.persistence.Project;
import timeitj.util.exceptions.InvitationFailedException;

/**
 *
 * @author kunee
 */
@DeclareRoles({"USERS","ADMINS"})
@Stateless
public class SessionFacadeBean implements SessionFacade {
    
    @EJB
    PersonManager personManager;
    
    @EJB
    ProjectManager projectManager;
    
    @EJB
    ParticipanceManager participanceManager;
    
    @EJB
    InvitationManager invitationManager;
    
    @Resource
    SessionContext sessContext;
    
    Logger logger = Logger.getLogger(SessionFacadeBean.class.getName());
    
    @RolesAllowed("USERS")
    public Person getCurrentUser() {
        String login = sessContext.getCallerPrincipal().getName();
        return personManager.getPersonByLogin(login);
    }
    
    @RolesAllowed("USERS")
    public List<Person> getAllUsers() {
        return personManager.getAllPersons();
    }
    
    @PermitAll
    public Person createPerson(Person person) {
        
        return personManager.create(person);
    }
    
    @RolesAllowed("USERS")
    public List<Project> getProjectsByOwner(Person owner) {
        return projectManager.getProjectsByOwner(owner);
    }
   
    @RolesAllowed("USERS")
    public Project createProject(Project project){
        logger.info("creating project '" + project.getName() + "'");
        project.setOwner(this.getCurrentUser());
        return projectManager.create(project);
    }
    
    @RolesAllowed("USERS")
    public Project updateProject(Project project){
        return projectManager.update(project);
    }
    
    @RolesAllowed("USERS")
    public void deleteProject(Project project) {
        logger.info("Deleting project '" + project.getName() + "'." );
        try {       
        projectManager.delete(project);
        }catch(Exception ex) {
            logger.error("Exception beim Loeschen: " + ex.getMessage());
        }
    }
    
    @RolesAllowed("USERS")
    public List<Participance> getParticipancesByPerson(Person person) {
        return participanceManager.getParticpipancesByPerson(person);
    }
    
    @RolesAllowed("USERS")
    public List<Participance> getParticipancesByProject(Project project) {
        return participanceManager.getParticpipancesByProject(project);
    }
    
    @RolesAllowed("USERS")
    public Participance createParticipance(Participance participance) {
        return participanceManager.create(participance);
    }
    
    @RolesAllowed("USERS")
    public void deleteParticipance(Participance participance) {
        participanceManager.delete(participance);
    }
    
    @RolesAllowed("USERS")
    public Invitation invitePerson(Person invitee, Project project) throws InvitationFailedException {
        Person inviter = getCurrentUser();
        
        if (inviter.equals(invitee)){
            logger.error(inviter.getLogin() + " tried to invite himself!");
            throw new InvitationFailedException("Can't invite Yourself!");
        }
        
        if (!inviter.equals(project.getOwner())){
            logger.error(inviter.getLogin() + " tried to invite somebody to a project he doesn't own!");
            throw new InvitationFailedException("Can't invite somebody to a project You don't own!");
        }
        
        Invitation invitation = new Invitation(inviter, invitee, project);
        invitation = invitationManager.create(invitation);
        return invitation;
    }
    
}
